<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #tableTitle>
        <!-- <a-button v-if="hasPermission('iot:dtu:add')" type="primary" preIcon="ant-design:plus-outlined" @click="handleAdd">新增</a-button> -->
        <a-button type="primary" preIcon="ant-design:plus-outlined" @click="handleAdd">新增</a-button>
        <!-- <a-button type="primary" preIcon="ant-design:export-outlined" @click="onExportXls"> 导出</a-button>
        <j-upload-button type="primary" preIcon="ant-design:import-outlined" @click="onImportXls">导入</j-upload-button> -->
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined" />
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button
            >批量操作
            <Icon icon="ant-design:down-outlined" />
          </a-button>
        </a-dropdown>
      </template>
      <template #status="{record, text}">
        <a-badge v-if="record.status=='online'" status="processing" :text="render.renderDict(text, 'iot_device_status')" />
        <a-badge v-else status="warning" :text="render.renderDict(text, 'iot_device_status')" />
      </template>
      <template #action="{ record }">
        <TableAction :actions="getActions(record)" :dropDownActions="getDropDownAction(record)"  />
      </template>
    </BasicTable>
    <ModbusModal @register="registerModal" @success="reload" />
    <!--调试抽屉-->
    <ModbusDebugDrawer @register="registerDrawer" />
    <DeviceChildDrawer @register="registerChildDrawer"/>
  </div>
</template>
<script lang="ts" name="iot-device" setup>
  import { ref } from 'vue';
  import { BasicTable, TableAction, ActionItem } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { list, deleteModbus, batchDeleteModbus, modbusConnect } from './modbus.api';
  import { columns, searchFormSchema } from './modbus.data';
  import ModbusModal from './ModbusModal.vue';
  import ModbusDebugDrawer from './ModbusDebugDrawer.vue';
  import DeviceChildDrawer from '../deviceChild/DeviceChildDrawer.vue';
  import { useDrawer } from '/@/components/Drawer';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { render } from '/@/utils/common/renderUtils';
  import { usePermission } from '/@/hooks/web/usePermission';
  const { createMessage } = useMessage();
  const [registerModal, { openModal }] = useModal();

  const { hasPermission } = usePermission();

  //注册drawer
  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerChildDrawer, { openDrawer: openChildDrawer }] = useDrawer();
  

  // 列表页面公共参数、方法
  const { prefixCls, onExportXls, onImportXls, tableContext } = useListPage({
    designScope: 'iot-dtu-template',
    tableProps: {
      title: 'Modbus设备列表',
      api: list,
      columns: columns,
      formConfig: {
        schemas: searchFormSchema,
      },
    },
  });

  const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;

  /**
   * 操作列定义
   * @param record
   */
  function getActions(record) {
    return [
      {
        label: '编辑',
        onClick: handleEdit.bind(null, record),
        ifShow: (_action) => {
          //return hasPermission('iot:dtu:edit');
          return true;
        },
      },
      {
        label: '子设备',
        onClick: handleChildDevice.bind(null, record),
        ifShow: (_action) => {
          //return hasPermission('iot:dtu:edit');
          return true;
        },
      },
    ];
  }

  /**
   * 下拉操作栏
   */
   function getDropDownAction(record): ActionItem[] {
    return [
    {
        label: '连接',
        popConfirm: {
          title: '是否连接选中MODBUS设备?',
          confirm: handleConnect.bind(null, record),
        },
        ifShow: (_action) => {
          return record.status=='offline' ;//&& hasPermission('iot:plc:connect');
        },
      },
      {
        label: '断开',
        popConfirm: {
          title: '是否断开选中MODBUS设备?',
          confirm: handleClose.bind(null, record),
        },
        ifShow: (_action) => {
          return record.status=='online' ;//&& hasPermission('iot:plc:connect');
        },
      },
      {
        label: '调试',
        onClick: handleDebug.bind(null, record),
        ifShow: (_action) => {
          // return hasPermission('iot:dtu:connect');
          return true;
        },
      },
      {
        label: '删除',
        popConfirm: {
          title: '是否确认删除',
          confirm: handleDelete.bind(null, record),
        },
        ifShow: (_action) => {
          //return hasPermission('iot:dtu:del');
          return true;
        },
      },
    ];
  }

  /**
   * 新增事件
   */
  function handleAdd() {
    openModal(true, {
      isUpdate: false,
    });
  }

  /**
   * 编辑事件
   */
  function handleEdit(record) {
    openModal(true, {
      record,
      isUpdate: true,
    });
  }

  /**
   * 子设备管理事件
   */
  function handleChildDevice(record) {
    openChildDrawer(true, {
      showFooter: true,
      record,
    });
  }

  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deleteModbus({ id: record.id }, reload);
  }

  /**
   * 批量删除事件
   */
  async function batchHandleDelete() {
    await batchDeleteModbus({ ids: selectedRowKeys.value }, reload);
  }

   /**
   * 连接
   */
   async function handleConnect(record){
    const params = Object.assign({}, record, {status: 'online'});
    const res = await modbusConnect(params);
    if(res.success){
      createMessage.success(res.msg);
      reload();
    }else{
      createMessage.error(res.msg);
    }
  }

  /**
   * 断开
   */
   async function handleClose(record){
    const params = Object.assign({}, record, {status: 'offline'});
    const res = await modbusConnect(params);
    if(res.success){
      createMessage.success(res.msg);
      reload();
    }else{
      createMessage.error(res.msg);
    }
  }

  /**
   * 调试
   */
  async function handleDebug(record) {
    openDrawer(true, {
      showFooter: false,
      record
    });
  }

</script>
